//package com.mzkj.wanjulaile.mobile.api.common.aspect;
//
//import com.mzkj.common.data.StatusCode;
//import com.mzkj.common.utils.StringUtils;
//import com.mzkj.wanjulaile.mobile.api.common.entity.ResultEntity;
//import com.mzkj.wanjulaile.mobile.api.modules.session.entity.Session;
//import com.mzkj.wanjulaile.mobile.api.modules.session.service.SessionService;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.AfterReturning;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.annotation.Pointcut;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import java.util.Arrays;
//import java.util.logging.Logger;
//
///**
// * accessToken刷新切面
// * 当accessToken失效，
// * 且request header中有refreshToken值时，
// * 自动刷新根据refreshToken刷新accessToken，
// * 并添加Session到ResultEntity里面
// * Created by wangxw on 2017/2/27.
// */
//@Aspect
//@Component
//public class AuthAspect {
//
//    private Logger logger = Logger.getLogger(this.getClass().getName());
//
//    @Resource
//    private SessionService sessionService;
//
//    private ThreadLocal<ResultEntity<Session>> session = new ThreadLocal<>();
//
//    @Pointcut("execution(public * com.mzkj.wanjulaile.mobile..controller.*AuthController.*(..))")
//    public void auth() {
//    }
//
//    @Before("auth()")
//    public void doBefore(JoinPoint joinPoint) throws Throwable {
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        String refreshToken = request.getHeader("X-ToyStory-Refresh-Token");
//        // 记录请求内容
//        logger.info("\n=============== AuthAspect doBefore ===========");
//        logger.info("REFRESH_TOKEN : " + refreshToken);
//        logger.info("URL : " + request.getRequestURL().toString());
//        logger.info("HTTP_METHOD : " + request.getMethod());
//        logger.info("IP : " + request.getRemoteAddr());
//        logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
//        logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
//        logger.info("\n=============== AuthAspect doBefore ===========\n");
//        String rs = (String) request.getAttribute("valid_access_token_rs");
//        if (StringUtils.isNotEmpty(refreshToken) && "2001".equals(rs)) {
//            ResultEntity<Session> re = sessionService.refreshToken(refreshToken);
//            session.set(re);
//        }
//    }
//
//    @AfterReturning(returning = "ret", pointcut = "auth()")
//    public void doAfterReturning(Object ret) throws Throwable {
//        logger.info("RESPONSE : " + ret);
//        ResultEntity re = (ResultEntity) ret;
//        if (StatusCode.SC0.getValue().equals(re.getStatusCode()) && session.get() != null) {
//            if (StatusCode.SC0.getValue().equals(session.get().getStatusCode())) {
//                re.setSession(session.get().getData());
//            } else {
//                re.setStatusCode(session.get().getStatusCode());
//                re.setMsg(session.get().getMsg());
//                re.setData(null);
//                re.setSession(null);
//            }
//        }
//    }
//}
